package com.mxpio.erp.equipment.controller;


import com.mxpio.erp.common.enums.EquipmentEnums;
import com.mxpio.erp.common.equipment.entity.ProjectExecutionProgress;
import com.mxpio.erp.common.equipment.entity.ProjectManagement;
import com.mxpio.erp.common.equipment.service.ProjectExecutionProgressService;
import com.mxpio.erp.common.equipment.service.ProjectManagementService;
import com.mxpioframework.common.util.SpringUtil;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.SnRuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "ProjectManagementController", description = "项目列表")
@RestController
@RequestMapping("/erp/equipment/project/management/")
public class ProjectManagementController {

    @Autowired
    private ProjectManagementService projectManagementService;

    @Autowired
    private ProjectExecutionProgressService projectExecutionProgressService;

    @GetMapping("list")
    @Operation(summary = "项目列表", description = "获取项目列表(无分页)", method = "GET")
    public Result<List<ProjectManagement>> list(@Parameter(description = "查询参数")Criteria criteria){
        List<ProjectManagement> list = projectManagementService.list(ProjectManagement.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "项目列表", description = "获取项目列表(分页)", method = "GET")
    public Result<Page<ProjectManagement>> page(@Parameter(description = "查询参数")Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        PageRequest page = PageRequest.of(pageNo - 1, pageSize);
        Page<ProjectManagement> pageList = projectManagementService.listPage(ProjectManagement.class, page, criteria);
        return Result.OK(pageList);
    }

    @GetMapping("list/{proCode}")
    @Operation(summary = "根据proCode获取项目列表", description = "根据proCode获取项目列表", method = "GET")
    public Result<ProjectManagement> getId(@PathVariable(name = "proCode", required = true) String proCode){
        ProjectManagement byId = projectManagementService.getById(ProjectManagement.class, proCode);
        return Result.OK(byId);
    }

    @RequestMapping("add")
    @Operation(summary =  "保存项目列表信息", description = "保存项目列表信息", method = "POST")
    public Result<ProjectManagement> add(@RequestBody ProjectManagement projectManagement){
        if (StringUtils.isBlank(projectManagement.getProCode())){
            SnRuleService snRuleService = SpringUtil.getBean(SnRuleService.class);
            String proCode = "PPA";
            String bizNo = (String) snRuleService.execute(proCode + "#####", null);
            projectManagement.setProCode(bizNo);
        }
        ProjectManagement saveProjectManagement = projectManagementService.save(projectManagement);
        projectManagementService.listExecute(projectManagement);// 生成状态记录
        return Result.OK(saveProjectManagement);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑项目列表", description = "编辑项目列表", method = "PUT")
    public Result<ProjectManagement> edit(@RequestBody ProjectManagement projectManagement){
        ProjectManagement updateProjectManagement = projectManagementService.update(projectManagement);
        return Result.OK(updateProjectManagement);
    }

    @DeleteMapping("remove/{proCode}")
    @Operation(summary = "根据proCode删除项目列表", description = "删除项目列表,多个项目用英文逗号分隔", method = "DELETE")
    public Result<ProjectManagement> remve(@PathVariable(name = "proCode", required = true)String proCode){
        String[] id = proCode.split(",");
        for (String key : id) {
            projectManagementService.delete(ProjectManagement.class, key);
        }
        return Result.OK();
    }

    @PutMapping("submit/{proCode}")
    @Operation(summary = "提交项目", description = "根据proCode提交项目", method = "PUT")
    public Result<ProjectManagement> submit(@PathVariable(name = "proCode", required = true) String proCode){
        ProjectManagement projectManagement = projectManagementService.getById(ProjectManagement.class, proCode);
        if (projectManagement.getProStatus().equals(EquipmentEnums.Project.PLAN.getCode())){
            projectManagementService.updateProStatus(projectManagement, EquipmentEnums.Project.LSSUED);
        }else {
            return Result.error("未找到需要提交的项目");
        }
        return Result.OK();
    }

    @PutMapping("execute/{proCode}")
    @Operation(summary = "为当前项目生成实际开始时间并修改状态", description = "为当前项目生成实际开始时间并修改状态", method = "PUT")
    public Result<ProjectManagement> execute(@PathVariable(name = "proCode", required = true) String proCode){
        ProjectManagement projectManagement = projectManagementService.getById(ProjectManagement.class, proCode);
        if (projectManagement.getProStatus().equals(EquipmentEnums.Project.LSSUED.getCode())){
            return projectManagementService.updateTime(projectManagement, EquipmentEnums.Project.IMPROGRESS);
        }else {
            return Result.error("该项目状态不在待执行, 不可执行");
        }
    }

    @PutMapping("updateSchedule/{proCode}")
    @Operation(summary = "根据proCode求总进度", description = "根据proCode求总进度", method = "PUT")
    public Result<ProjectManagement> updateSchedule(@PathVariable(name = "proCode", required = true) String proCode){
        Criteria criteria = Criteria.create();
        criteria.addCriterion("proCode", Operator.EQ,proCode);
        List<ProjectExecutionProgress> ProjectExecutionProgress = projectExecutionProgressService.list(ProjectExecutionProgress.class, criteria);
        int itemSpeed = 0;
        for (ProjectExecutionProgress projectExecutionProgress : ProjectExecutionProgress) {
            Integer itemSpeed1 = projectExecutionProgress.getItemSpeed();
            itemSpeed += itemSpeed1;
        }
        ProjectManagement pmList = projectManagementService.getById(ProjectManagement.class, proCode);
        pmList.setSchedule(itemSpeed);
        ProjectManagement update = projectManagementService.update(pmList);
        if (update.getSchedule() == 100){
            return projectManagementService.updateRealyEndTime(update, EquipmentEnums.Project.IMPROGRESS);
        }
        return Result.OK();
    }
}
